
/* setupbox.cpp by GED @ fortis */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "plugin.h"
#include "setupbox.h"
#include "infobox.h"

typedef enum {
	setup_item_timeout,
	setup_item_navigation,
	n_setup_item,
} T_SetupItem;

#define setup_x		((OSDWIDTH-setup_w)/2)
#define setup_y		((OSDHEIGHT-setup_h)/2)
#define setup_w		(8+setup_item_w+8)
#define setup_h		(8+((setup_item_h+setup_item_v_tab)*n_setup_item)+4)

#define setup_item_x			(setup_x+8)
#define setup_item_y			(setup_y+8)
#define setup_item_w			240
#define setup_item_h			30
#define setup_item_v_tab		2

#define setup_bc		ARGB(0xF0, 57, 34, 54)
#define setup_tc		ARGB(0xFF, 245, 248, 250)
#define setup_tbc	ARGB(0xF0, 36, 21, 34)
#define setup_abc	ARGB(0xF0, 62, 61, 71)

C_SetupBox::C_SetupBox()
{
	S_InfoCfg cfg = {0,};

	SendCommand( PCMD_BlockKey, (dword)-1 );		// all key block.
	SaveState( STATEIDX_Sub, SUBSTATE_PluginPop );	// set substate to pluginpop

	m_scrBuf = OsdCompress( setup_x, setup_y, setup_w, setup_h );

	CfgParse( cfg );

	m_timeout = cfg.timeout;
	m_navigation = cfg.navigation;

	m_item = setup_item_timeout;
	Draw();
}

C_SetupBox::~C_SetupBox()
{
	FILE *fp;
	fp = fopen( INFO_CFGFILE, "w+" );
	if( fp )
	{
		char *buf = GetStrBuf();
		sprintf( buf, "T: %d\nN: %d\n", m_timeout, m_navigation );
		fputs( buf, fp );
		fclose( fp );
	}

	OsdDecompress( m_scrBuf, setup_x, setup_y, setup_w, setup_h );
	free( m_scrBuf );

	RestoreState( STATEIDX_Sub );
	SendCommand( PCMD_ReleaseKey, (dword)-1 );
}

int C_SetupBox::RcuCtrl( int key )
{
	switch( key )
	{
		case REM_UP :
		case REM_DOWN :
			{
				int prvItem = m_item;
				if( key == REM_UP )
					m_item = (m_item + (n_setup_item-1)) % n_setup_item;
				else
					m_item = (m_item + 1) % n_setup_item;
				DrawItem(prvItem);
				DrawItem(m_item);
			}
			break;
		case REM_RIGHT :
		case REM_LEFT :
			{
				switch( m_item )
				{
					case setup_item_timeout :
						if( key == REM_RIGHT )
							m_timeout = (m_timeout + 1) % (INFO_TIMEOUT_MAX+1);
						else
							m_timeout = (m_timeout + INFO_TIMEOUT_MAX) % (INFO_TIMEOUT_MAX+1);
						break;
					case setup_item_navigation :
						m_navigation = !m_navigation;
						break;
				}
				DrawItem(m_item);
			}
			break;
		case REM_MENU :
		case REM_OK :
		case REM_EXIT :
			return 1;
	}
	return 0;
}

void C_SetupBox::Draw()
{
	FillBox( setup_x, setup_y, setup_w, setup_h, setup_bc );
	for( int item=0; item<n_setup_item; item++ ) DrawItem( item );
}

void C_SetupBox::DrawItem( int item )
{
	dword bc = setup_tbc;
	char *strBuf = GetStrBuf();
	switch( item )
	{
		case setup_item_timeout :
			if( m_timeout == 0 )
				strcpy( strBuf, "timeout :  <  always  >");
			else
				sprintf( strBuf, "timeout :  <  %d sec  >", m_timeout);
			break;
		case setup_item_navigation :
			sprintf( strBuf, "navigation :  <  %s  >", (m_navigation ? "on" : "off") );
			break;
	}
	if( item == m_item ) bc = setup_abc;
	DrawText( setup_item_x, setup_item_y+((setup_item_h+setup_item_v_tab)*item), setup_item_w, setup_item_h, \
		strBuf, setup_tc, bc, ALIGN_Left, ALIGN_Center, 10 );
}

